<template>
  <el-card class="w-full pt-[5px]">
    <el-form :model="searchForm" class="mt-[20px]" label-width="110px" ref="searchFormRef">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="经费卡号" class="mb-[0px]" prop="feeCardNum">
            <el-input v-model="searchForm.feeCardNum" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称" class="mb-[0px]" prop="projectName">
            <el-input v-model="searchForm.projectName" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="经费卡负责人" class="mb-[0px]" prop="feeChargerName">
            <el-input v-model="searchForm.feeChargerName" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="flex justify-end">
            <el-button type="primary" @click="searchFormOpen = !searchFormOpen">
              更多
              <el-icon color="#fff">
                <CaretTop v-show="searchFormOpen" />
                <CaretBottom v-show="!searchFormOpen" />
              </el-icon>
            </el-button>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="mt-[20px]" v-show="searchFormOpen">
        <el-col :span="6">
          <el-form-item label="来款类型" prop="incomeType">
            <el-select v-model="searchForm.incomeType" clearable>
              <el-option v-for="item in incomeTypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目分类" prop="projectType">
            <CustomTreeSelect paramsName="项目分类（所有-有级联）" v-model="searchForm.projectType" valueKey="id"
              labelKey="label" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所在单位" prop="unitId">
            <CustomTreeSelect v-model="searchForm.unitId" valueKey="codeColumn" labelKey="valueColumn" :isFetch="false"
              :dataList="unitList" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="来款单位" prop="incomeUnitName">
            <el-input v-model="searchForm.incomeUnitName" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="对冲号" prop="dch">
            <el-input v-model="searchForm.dch" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="0">
            <el-input v-model="searchForm.params.minfee" placeholder="最小本次入账" clearable
              style="width: calc(50% - 10px)" />
            <span style="width: 20px; text-align: center">至</span>
            <el-input v-model="searchForm.params.maxfee" placeholder="最大本次入账" clearable
              style="width: calc(50% - 10px)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label-width="45">
            <CommonDateSelect placeholder="到账日期" v-model:beginTime="searchForm.params.beginTime"
              v-model:endTime="searchForm.params.endTime" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>

  <el-card class="w-full pt-[5px] mt-[10px]">
    <div class="mb-[20px]">
      <el-button v-if="false" type="primary" @click="handleAdd">
        <el-icon class="mr-[5px]">
          <Plus />
        </el-icon>
        新增
      </el-button>
      <el-button @click="handleExport"> <svg-icon iconClass="export" class="mr-[5px]"></svg-icon>导出 </el-button>
    </div>
    <el-table :data="tableData" class="w-full" stripe row-key="id" ref="tableRef"
      @sort-change="sortChange" >
      <el-table-column prop="feeCardNum" label="经费卡号" sortable="c.fee_card_num"/>
      <el-table-column prop="projectName" label="项目名称" sortable="p.name">
        <template #default="{ row }">
          <el-link type="primary" @click="handleShow(row.projectId)">{{ row.projectName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="feeChargerName" label="经费卡负责人" sortable="c.fee_charger_name"/>
      <el-table-column prop="incomeDate" label="到账日期" sortable="c.income_date"/>
      <el-table-column prop="incomeFee" label="本次入账(万元)" sortable="c.income_fee">
        <template #default="{ row }">
          <el-link type="primary" @click="handleView(row)">{{ FloatDiv(row.incomeFee, 10000) }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="unitName" label="所属单位" sortable="bu.name"/>
      <el-table-column fixed="right" label="操作" width="195" align="center">
        <template #default="{ row }">
          <el-button type="primary" size="small" plain v-if="false&&row.bizType=='directIncome'" @click="handleEdit(row)"> 编辑 </el-button>
          <el-button type="primary" size="small" plain @click="handleView(row)"> 查看 </el-button>
          <el-button type="primary" size="small" plain v-if="false&&row.bizType=='directIncome'" @click="handleDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-[20px] flex justify-end">
      <el-pagination v-bind="pagination" @update:current-page="currentChange" @update:page-size="sizeChange" />
    </div>
  </el-card>
  <EditModal ref="editModalRef" @save-success="search" />
  <ViewModal ref="viewModalRef" @save-success="search" />
  <ViewProjectModal ref="viewProjectRef" />
</template>

<script setup>
import useTable from "@/hooks/useTable";
import CommonDateSelect from "@/views/fee/components/CommonDateSelect.vue";
import CommonLabel from "@/views/fee/components/CommonLabel.vue";
import CustomTreeSelect from "@/views/project/components/CustomTreeSelect.vue";
import EditModal from "./components/EditModal.vue";
import ViewModal from "./components/ViewModal.vue";
import ViewProjectModal from "@/views/project/zx/lxxm/components/ViewModal.vue";
import { getList,delproclaim } from "@/api/fee/rz/jfkrz/index.js";
import { getCommonData } from "@/api/project/common.js";
import useCommonFetch from "@/hooks/useCommonFetch";
import { FloatDiv } from "@/utils/pub";
import { exportFile } from "@/api/fee/common.js";
import { downloadFile } from "@/utils/main";
import { ref } from "vue";
const editModalRef = ref();
const viewModalRef = ref();
const viewProjectRef = ref();
const searchFormOpen = ref(false);
const searchFormRef = ref();
const tableRef = ref();
const incomeTypeList = useCommonFetch("来款类型"); //来款类型
const unitList = useCommonFetch("成果归属单位"); //所有单位; 
const searchForm = ref({
  dch: "",
  feeCardNum:"",
  incomeType:"",
  incomeUnitName:"",
  feeChargerName: "",
  orderByColumn:"c.income_date",
  isAsc:"descending",
  unitId: [], 
  projectName: "", 
  projectType:[],
  params:{beginTime: "",endTime: "",maxfee:"",minfee:"",},
});

const { loading, pagination, sizeChange, currentChange, tableData, handleDelete, search } =
useTable((pagin) => 
  getList({
      pageNum: pagin.pageNum,
      pageSize: pagin.pageSize,
      ...searchForm.value,
      projectType: searchForm.value.projectType.join(","),
      unitId: searchForm.value.unitId.join(","),
    })
  );
//点击重置
const resetForm = () => {
  searchFormRef.value.resetFields();
  searchForm.value.params.beginTime = "";
  searchForm.value.params.endTime = "";
  searchForm.value.params.maxfee = "";
  searchForm.value.params.minfee = "";
  search();
};
const handleAdd = () =>{
  editModalRef.value.edit();
}

//点击编辑
const handleEdit = (row) => {
  editModalRef.value.edit(row);
};

//查看页面
const handleView = (row)=>{
  viewModalRef.value.view(row);
}
//删除
const handleDel = (id) => {
  handleDelete(delproclaim, [id]);
};

//排序
const sortChange =(row)=>{
 searchForm.value.orderByColumn = row.column.sortable;
 searchForm.value.isAsc=row.order;
 search();
}

/** 导出按钮操作 */
const handleExport = () => {
  let params = {...searchForm.value,
    projectType: searchForm.value.projectType.join(","),
    unitId: searchForm.value.unitId.join(",")};
  exportFile('/feemanage/proincome/export',params).then((res) => {
    downloadFile(res, `经费到账清单_${new Date().getTime()}.xlsx`);
  }).catch((error) => {
    console.log(error);
  }); 
}
//点击项目名称
const handleShow = (id) => {
  viewProjectRef.value.view(id);
};
</script>

<style scoped lang="scss"></style>